<template>
  <kr-print-designer ref="kr" :temp-value="value" :widget-options="widgets" @save="handleSave" />
</template>
<script>
import { OutStockOptions, InStockOptions } from '../data/options'
export default {
  data() {
    return {
      index: '',
      tempList: [], // 所有模板
      value: {
        title: 'demo',
        width: 750,
        height: 550,
        pageWidth: 750,
        pageHeight: 550,
        tempItems: [],
      },
      widgets: OutStockOptions,
    }
  },
  async created() { // 同步
    this.index = this.$route.query.index
    try {

      var res = await this.$http.get('/test/getAll')
      console.log(res.data)
      for (let i = 0; i < res.data.length; i++) {
        this.tempList.push(JSON.parse(res.data[i].detail))
      }
      this.value = this.tempList[this.index]
      this.widgets = this.value.type == 1 ? OutStockOptions : InStockOptions
      this.$refs.kr.update(this.value, this.widgets)

      console.log("this.widgets")
      console.log(this.widgets)

    } catch (err) {
      console.error(err)
    }
  },
  methods: {
    handleSave(data) {
      this.$http.get('/test/getAll')
        .then(res=>{
          this.id = res.data[this.index].id
          let para = {
            id: this.id,
            detail:JSON.stringify(data)
          };
          this.$http.post("/test/edit",para).then()
            .catch(result=>{
              alert("系统错误！");
            })
        })
        .catch(function (error) {
          console.log(error);
        });
      // localStorage.setItem('tempList', JSON.stringify(tempList))
      this.$router.back()
    },
  },
  /*created() {
    this.index = this.$route.query.index
    try {
      let tempList = JSON.parse(localStorage.getItem('tempList')) || []
      this.value = tempList[this.index]
      this.widgets = this.value.type == 1 ? OutStockOptions : InStockOptions
    } catch (err) {
      console.error(err)
    }
  },
  methods: {
    handleSave(data) {
      // 查询所有
      let tempList = JSON.parse(localStorage.getItem('tempList')) || []
      tempList[this.index] = data
      localStorage.setItem('tempList', JSON.stringify(tempList))
      this.$router.back()
    },
  },*/
}
</script>
